<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%//@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="ui" uri="http://egovframework.gov/ctl/ui"%>
<%@ taglib prefix="validator" uri="http://www.springmodules.org/tags/commons-validator" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="ajax" uri="http://ajaxtags.sourceforge.net/tags/ajaxtags" %>
<%@ taglib prefix="tms" uri="http://www.able_tag_support.com/tag" %>
<html>
<head>

<script type="text/javascript" src="<c:url value='/js/com/JXParser.js'/>"></script>
<script type="text/javascript" src="<c:url value='/js/com/JDocument.js'/>"></script>
<script type="text/javascript" src="<c:url value='/js/com/JPrototype.js'/>"></script>
<script type="text/javascript" src="<c:url value='/js/com/ajax/prototype.js' />"></script>
<script type="text/javascript" src="<c:url value='/js/com/ajax/scriptaculous/scriptaculous.js' />"></script>
<script type="text/javascript" src="<c:url value='/js/com/ajax/overlibmws/overlibmws.js' />"></script>
<script type="text/javascript" src="<c:url value='/js/com/ajax/ajaxtags.js' />"></script>
<script type="text/javascript" src="<c:url value='/js/com/Common.js'/>"></script>

<script language="javascript">
	this.title="달력";
	var gcGray = "#808080";
	var gcToggle = "F2EBD6";
	var gcBG = "#FFFFFF";

	/**기존 input value **/
	var gdCurDate	= new Date();
	var giYear		= gdCurDate.getFullYear();
	var giMonth		= gdCurDate.getMonth()+1;
	var giDay		= gdCurDate.getDate();

	/** 실제 현재 년도 **/
	var rCrDate		= new Date();

	function setCurrentDate(yyyy,mm,dd){
		gdCurDate	= new Date(yyyy,parseInt(mm)-1,dd);
		giYear		= gdCurDate.getFullYear();
		giMonth		= gdCurDate.getMonth()+1;
		giDay		= gdCurDate.getDate();
	}

	function onstart() {
		var crdate	= opener.getCurrentDate();
		crdate		= crdate.replaceAll("-","").replaceAll(".","").replaceAll("/","");
		if(crdate.length==8){
			setCurrentDate(crdate.substring(0,4),crdate.substring(4,6),crdate.substring(6));
		}

		var tbSelYear	= new JSelect(document.getElementByName("tbSelYear"));
		var tbSelMonth	= new JSelect(document.getElementByName("tbSelMonth"));

		for(var i=parseInt(rCrDate.getFullYear())-100;i<parseInt(rCrDate.getFullYear())+3;i++)
			tbSelYear.addItem(i,i+"년");

		for (var j=0; j<12; j++)
			tbSelMonth.addItem(j+1,gMonths[j]);

		fDrawCal(giYear, giMonth,35, 12);
		fSetYearMon(giYear, giMonth);


	}

	function fSetDate(iYear, iMonth, iDay){
		iMonth = gn_lpad(iMonth,0,2);
		iDay = gn_lpad(iDay,0,2);

		opener.setCalendarDate(iYear,iMonth,iDay);
		self.close();
		//window.returnValue=iYear+""+iMonth+""+iDay;
		//window.close();
	}

	function gn_lpad(Rstring,pad_str,base_len){
		 var Rstring = Rstring.toString();
		 var str_len = Rstring.length;

		for(  var i=str_len ; i<= base_len; i++){

			   if (str_len != base_len)  {
			Rstring = pad_str + Rstring;
						str_len = Rstring.length;

			  }
		}

		return Rstring;
	}

	function fSetSelected(aCell){
	  var iOffset = 0;
	  var iYear		= parseInt(document.getElementByName("tbSelYear").value);
	  var iMonth	= parseInt(document.getElementByName("tbSelMonth").value);

	  aCell.bgColor = gcBG;
	  with (aCell.children["cellText"]){
			  var iDay = parseInt(innerHTML);
			if (color=="#b7b7b7"){
				return;
			}

			if (iMonth<1) {
				iYear--;
				iMonth = 12;
			}else if (iMonth>12){
					iYear++;
					iMonth = 1;
			}
	  }
	  fSetDate(iYear, iMonth, iDay);
	}


	//날짜 format 만들기
	function fBuildCal(iYear, iMonth) {
	  var aMonth=new Array();
	  for(i=1;i<7;i++)
			  aMonth[i]=new Array(i);

	  var dCalDate=new Date(iYear, iMonth-1, 1);
	  var iDayOfFirst=dCalDate.getDay();
	  var iDaysInMonth=new Date(iYear, iMonth, 0).getDate();
	  var iOffsetLast=new Date(iYear, iMonth-1, 0).getDate()-iDayOfFirst+1;
	  var iDate = 1;
	  var iNext = 1;

	  for (d = 0; d < 7; d++)
			aMonth[1][d] = (d<iDayOfFirst)?-(iOffsetLast+d):iDate++;
	  for (w = 2; w < 7; w++)
		  for (d = 0; d < 7; d++)
				aMonth[w][d] = (iDate<=iDaysInMonth)?iDate++:-(iNext++);
	  return aMonth;
	}


	// 달력 Drawing
	function fDrawCal(iYear, iMonth, iCellWidth, iDateTextSize) {
		var WeekDay = new Array("일","월","화","수","목","금","토");
		var selClass = new Array("txt_car03","txt_car01","txt_car01","txt_car01","txt_car01","txt_car01","txt_car02");
		var styleTD = " bgcolor='"+gcBG+"' width='"+iCellWidth+"' bordercolor='"+gcBG+"' valign='middle' align='center'  style='font:"+iDateTextSize+" Courier;";            //Coded by Liming Weng(Victor Won)  email:victorwon@netease.com
		var drawCalStr	= "<table width='100%' height='100%' border='1' cellspacing='0' cellpadding='0'>"
						+"<tr align=center>"
						+"</tr>";

		for (w = 1; w < 7; w++) {
			drawCalStr+="<tr align='center'>";
			for (d = 0; d < 7; d++) {
				drawCalStr+="<td height=22 id=calCell "+styleTD+"font-family: '돋음', 'Arial'; font-size: 9pt; font-weight: bold; line-height:15px;cursor:hand;' onMouseOver='this.bgColor=gcToggle' onMouseOut='this.bgColor=gcBG' onclick='fSetSelected(this)' align=center>"
					+"<font id='cellText'> </font>"
					+"</td>"
			}
			drawCalStr+="</tr>";
			if((w == 6)&&(d == 7)){
			}else{
				drawCalStr+="<td colspan='7' bgcolor='#C5DAA1' height=1><img src='<c:url value='/images/calendar/blank.gif'/>' width='1' height='1'  border='0'></td>";
			}
		}

		drawCalStr+="</table>";

		document.getElementById("drawCal").innerHTML	= drawCalStr;
	}

	// 날짜 setting
	function fUpdateCal(iYear, iMonth) {
		myMonth = fBuildCal(iYear, iMonth);
		var i = 0;
		var cellTxt;
		for (w = 0; w < 6; w++){
			for (d = 0; d < 7; d++){
				cellTxt	= document.getElementsById("cellText")[(7*w)+d];
				cellTxt.Victor = i++;
				if (myMonth[w+1][d]<0) {
					cellTxt.color = "#B7B7B7";
					cellTxt.innerHTML = -myMonth[w+1][d];
				}else{
					if(d==0){
						cellTxt.color="#FF4800";
					}else if(d==6){
						cellTxt.color="#0061A3";
					}else{
						cellTxt.color="#363636";
					}
					cellTxt.innerHTML = myMonth[w+1][d];
				}
			}
		}
	}

	// 년,월 select  setting
	function fSetYearMon(iYear, iMon){
		document.getElementByName("tbSelMonth").options[iMon-1].selected = true;
		for (i = 0; i < document.getElementByName("tbSelYear").options.length; i++)
			if (document.getElementByName("tbSelYear").options[i].value == iYear)
				document.getElementByName("tbSelYear").options[i].selected = true;

		fUpdateCal(iYear, iMon);
	}

	function fPrevMonth(){
	  var iMon = document.getElementByName("tbSelMonth").value;
	  var iYear = document.getElementByName("tbSelYear").value;

	  if (--iMon<1) {
			  iMon = 12;
			  iYear--;
	  }

	  fSetYearMon(iYear, iMon);
	}

	function fNextMonth(){
	  var iMon = document.getElementByName("tbSelMonth").value;
	  var iYear = document.getElementByName("tbSelYear").value;
	  if (++iMon>12) {
			  iMon = 1;
			  iYear++;
	  }

	  fSetYearMon(iYear, iMon);
	}


	var gMonths = new Array("1월","2월","3월","4월","5월","6월","7월","8월","9월","10월","11월","12월");

</script>

<title>날짜선택</title>
<meta http-equiv='Content-Type' content='text/html; charset=euc-kr'>
<style type='text/css'>
.button{
	cursor:hand;
	color:#363636;
	height:22px;
	width:70px;
	background:#FFFFFF;
	border-width: 2px;
	border-style: solid;
	border-color: #CCCCCC #B6B6B6 #B6B6B6 #CCCCCC;
}
</style>
</head>

<body onload='javascript:onstart()' bgcolor='#EAF3DA' leftmargin='1' topmargin='1' marginwidth='0' marginheight='0'>
<table width='210' border='0' cellspacing='0' cellpadding='0'>
  <tr>
    <td valign='bottom'><table width='210' border='0' cellspacing='0' cellpadding='0'>
        <tr>
          <td align='center'><table width='185' height='30' border='0' cellspacing='0' cellpadding='0' bgcolor='#EAF3DA'>
              <tr>
   <td align='left'><img src="<c:url value='/images/calendar/goback.gif'/>",width='15',height='15', OnClick='javascript:fPrevMonth();' >
   <td align='center'>
 <SELECT name='tbSelYear' onChange='javascript:fUpdateCal(document.getElementByName("tbSelYear").value, document.getElementByName("tbSelMonth").value)' Victor='Won' class='button'>
</SELECT>
				  </td>
                <td align='center'>
 <select name='tbSelMonth' onChange='javascript:fUpdateCal(document.getElementByName("tbSelYear").value, document.getElementByName("tbSelMonth").value)' Victor='Won'  class='button'>
</SELECT>
				  </td>
   <td align='left'><img src='<c:url value='/images/calendar/go.gif'/>',width='15',height='15', OnClick='javascript:fNextMonth();' >
</td>
              </tr>
            </table></td>
        </tr>
      </table> </td>
  </tr>
  <tr>
    <td height='23'><img src='<c:url value='/images/calendar/day.gif'/>' width='210' height='23'></td>
  </tr>
</table>
<table width='210' >
<tr>
<td align='center'>
<DIV id='drawCal' style='background-color:#EDF4F4;'>
</DIV>
</td>
</tr>
</table>
</body>

</html>